#include "muduo/proto/dispatcher.h"
#include "muduo/proto/codec.h"

#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpClient.h"
#include "muduo/net/EventLoopThread.h"
#include "muduo/base/CountDownLatch.h"
#include "request.pb.h"

#include <iostream>

google::protobuf::Message *messageToSend;

class Client
{
public:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<kky::TranslateResponse> TranslateResponsePtr;
    typedef std::shared_ptr<kky::AddResponse> AddResponsePtr;
    Client(const std::string &server_ip, int server_port)
        : _latch(1), _client(_loopthread.startLoop(), muduo::net::InetAddress(server_ip, server_port), "Client"),
          _dispatcher(std::bind(&Client::onUnknownMessage, this, std::placeholders::_1,
                                std::placeholders::_2, std::placeholders::_3)),
          _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher,
                           std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        // 注册业务请求处理函数
        _dispatcher.registerMessageCallback<kky::TranslateResponse>(std::bind(&Client::onTranslate, this, std::placeholders::_1,
                                                                              std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<kky::AddResponse>(std::bind(&Client::onAdd, this, std::placeholders::_1,
                                                                        std::placeholders::_2, std::placeholders::_3));

        _client.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec, std::placeholders::_1,
                                             std::placeholders::_2, std::placeholders::_3));
        _client.setConnectionCallback(std::bind(&Client::onConnection, this, std::placeholders::_1));
    }

    // 连接服务器———需要阻塞等待连接建立成功之后再返回
    void connect()
    {
        _client.connect();
        _latch.wait(); // 阻塞等待，直到连接建立成功
    }

    void Translate(const std::string &msg)
    {
        kky::TranslateRequest req;
        req.set_msg(msg);
        send(&req);
    }

    void Add(const int num1, int num2)
    {
        kky::AddRequest req;
        req.set_num1(num1);
        req.set_num2(num2);
        send(&req);
    }

private:

    bool send(const google::protobuf::Message *message)
    {
        if (_conn->connected()) // 连接状态正常，再发送，否则返回false
        {
            _codec.send(_conn, *message);
            return true;
        }
        return false;
    }
    
    void onUnknownMessage(const muduo::net::TcpConnectionPtr &, const MessagePtr &message, muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
    }

    void onTranslate(const muduo::net::TcpConnectionPtr &conn, const TranslateResponsePtr &message, muduo::Timestamp)
    {
        LOG_INFO << "翻译结果: " << message->msg();
    }

    void onAdd(const muduo::net::TcpConnectionPtr &conn, const AddResponsePtr &message, muduo::Timestamp)
    {
        LOG_INFO << "加法计算结果: " << message->result();
    }

    void onConnection(const muduo::net::TcpConnectionPtr &conn)
    {
        LOG_INFO << conn->localAddress().toIpPort() << " -> "
                 << conn->peerAddress().toIpPort() << " is "
                 << (conn->connected() ? "UP" : "DOWN");

        if (conn->connected())
        {
            _latch.countDown(); // 唤醒主线程中的阻塞
            _conn = conn;
        }
        else
        {
            // 清理当前的连接状态  增强健壮性
            _conn.reset();
        }
    }

private:
    muduo::CountDownLatch _latch;            // 实现同步
    muduo::net::EventLoopThread _loopthread; // 异步循环处理线程
    muduo::net::TcpConnectionPtr _conn;      // 客户端对于的链接
    muduo::net::TcpClient _client;           // 客户端
    ProtobufDispatcher _dispatcher;          // 请求分发器
    ProtobufCodec _codec;                    // 协议处理器
};

int main()
{
    Client client("127.0.0.1",8085);
    client.connect();

    client.Translate("hello");
    client.Add(111,33);
    sleep(1);
    return 0;
}